﻿/*
Copyright © 2011 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
*/

using System;
using System.IO;
using System.IO.IsolatedStorage;

namespace Simple.Storage.Api
{
    /// <summary>
    /// Provides helper methods to work with <see cref="IsolatedStorageFile"/>.
    /// </summary>
    public static class IsolatedStorageHelper
    {
        /// <summary>
        /// Combines a variable number of paths.
        /// </summary>
        /// <param name="root">The root path.</param>
        /// <param name="paths">The additional paths.</param>
        /// <returns>The combination of all the paths.</returns>
        /// <exception cref="ArgumentNullException">
        /// One of the parameters is <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// One of the parameters contains illegal characters.
        /// </exception>
        /// <seealso cref="Path.Combine(string, string)"/>
        public static string Combine(string root, params string[] paths)
        {
            if (root == null)
            {
                throw new ArgumentNullException("root");
            }
            else if (paths == null)
            {
                throw new ArgumentNullException("paths");
            }

            string result = root;

            foreach (string path in paths)
            {
                result = Path.Combine(result, path);
            }

            return result;
        }

        /// <summary>
        /// Deletes a directory and all its sub-elements.
        /// </summary>
        /// <param name="dir">The directory to delete.</param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="dir"/> parameter is <c>null</c>.
        /// </exception>
        public static void DeleteDirectory(string dir)
        {
            if (dir == null)
            {
                throw new ArgumentNullException("dir");
            }

            using (IsolatedStorageFile store = StorageFactory.Create())
            {
                if (!store.DirectoryExists(dir))
                {
                    // The folder doesn't exist: nothing to do
                    return;
                }

                foreach (string subdir in store.GetDirectoryNames(Path.Combine(dir, "*")))
                {
                    DeleteDirectory(Path.Combine(dir, subdir));
                }

                foreach (string file in store.GetFileNames(Path.Combine(dir, "*")))
                {
                    store.DeleteFile(Path.Combine(dir, file));
                }

                if (!string.IsNullOrEmpty(dir))
                {
                    store.DeleteDirectory(dir);
                }
            }
        }
    }
}
